import { login, loginOut, getPower } from '@/api/user';
import { getToken, setToken, removeToken, getImgSrc, setImgSrc, removeImgSrc, getRoleCodeList, setRoleCodeList, removeRoleCodeList } from '@/utils/auth';
import { resetRouter } from '@/router';
// 导入静态路由
import { constantRoutes } from '@/router';

const state = {
	token: getToken(),
	name: '',
	avatar: '',
	tokenName: '数字资产',
	imgBaseUrl: getImgSrc(),
	routes: constantRoutes,
	sysUser: {},
	roleCodeList: [],
	userId: ''
	// imgBaseUrl: 'http://8.141.7.66/static'
	// imgBaseUrl: '/api/erp/static'
};
const mutations = {
	setToken(state, token) {
		state.token = token;
		// 同步到缓存
		setToken(token);
	},
	removeToken(state) {
		// 删除Vuex的token
		state.token = null;
		removeToken();
		removeRoleCodeList();
		removeImgSrc();
	},
	setName(state, name) {
		state.name = name;
	},
	setAvatar(state, avatar) {
		state.avatar = avatar;
	},
	setimgBaseUrl(state) {
		state.imgBaseUrl = 'https://oss.yukukeji.com/';
		setImgSrc(state.imgBaseUrl);
	},
	// 定义修改用户信息的方法
	setUserInfo(state, userInfo) {
		state.sysUser = userInfo.sysUser;
		state.name = userInfo.sysUser.employeeName;
		state.roleCodeList = userInfo.roleCodeList;
		setRoleCodeList(userInfo.roleCodeList);
	},
	// 修改用户可以访问的路由规则
	setRoutes(state, newRoutes) {
		// 合并静态路由和可以访问的动态路由
		state.routes = [...constantRoutes, ...newRoutes];
	}
	// getRoutes(state, newRoutes) {
	//   // 合并静态路由和可以访问的动态路由
	//   state.routes = [...constantRoutes, ...newRoutes]
	// }
};
const actions = {
	setimgBaseUrl(context) {},
	// context上下文，传入参数
	async login(context, data) {
		// console.log(data)
		// todo: 调用登录接口
		const res = await login(data);
		if (res.code === 200) {
			console.log(res);
			// 返回一个token 123456
			context.commit('setimgBaseUrl');
			context.commit('setToken', res.data.token);
		} else {
			return res.code;
		}
	},
	// 封装调用接口的方法
	async getUserInfo(context) {
		// context.state.userInfo.userId = '12345';
		// 调用接口 返回用户基本信息
		const result = await getPower();
		console.log(result);
		if (result.code === 200) {
			context.commit('setUserInfo', result.data);
		}
		// 返回用户信息
		// return result.data
	},
	// 退出登录
	async logOut(context) {
		// context.commit('setUserInfo', {});
		// return;
		const res = await loginOut();
		if (res.code === 200) {
			context.commit('removeToken');
			resetRouter();
			context.commit('setUserInfo', {
				sysUser: {
					id: '',
					employeeName: ''
				},
				roleCodeList: []
			});
			context.commit('removeToken');
			//   // 删除token
			//   // 删除用户信息
			// 把动态路由清掉
			context.commit('setRoutes', []);
			// 清除路由
		} else {
			// Message.error(res.message)
			console.log(res.message);
			return res.code;
		}
	},
	removeUserInfo(context) {
		context.commit('removeToken');
		resetRouter();
		context.commit('setUserInfo', {
			sysUser: {
				id: '',
				employeeName: ''
			},
			roleCodeList: []
		});
		context.commit('removeToken');
		//   // 删除token
		//   // 删除用户信息
		// 把动态路由清掉
		context.commit('setRoutes', []);
	},
	async getInfo(context) {
		// const res = await getInfo(context.state.token)
		// console.log(res)
		// context.commit('setName', res.data.name)
		// context.commit('setAvatar', res.data.avatar)
	}
};
export default {
	namespaced: true,
	state,
	mutations,
	actions
};
